﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using KerriganDataContract.Blog;
using System.Data.SqlClient;
using System.Data;
using KerriganDataContract;

namespace KerriganBlog
{
    public partial class ManageArticleHome : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                
                using (ServiceProxy Srv = new ServiceProxy())
                {


                    //PagedDataSource page = new PagedDataSource();
                    //page.AllowCustomPaging = true;
                    //page.AllowPaging = true;
                    //page.DataSource = Srv.Service.GetArticleToDisplay();
                    //page.PageSize = 10;

                    GridView1.DataSource = Srv.Service.GetArticleToDisplay();
                    GridView1.DataBind();

                    buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
                    btn_add_Click(sender, e);
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "returnToParent();", true);    

                }


            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                  vwGetArticle article =  e.Row.DataItem as vwGetArticle ;
                  (e.Row.FindControl("lnkEdit") as LinkButton).Attributes.Add("onClick", "ShowEditModal('" + article.IdBlogArticle + "');");
                  (e.Row.FindControl("lnkDelete") as LinkButton).CommandArgument = article.IdBlogArticle.ToString();
              }
         }



        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyDelete")
            {
                using (ServiceProxy Srv = new ServiceProxy())
                {
                    int ID = Convert.ToInt32(e.CommandArgument);
                    Srv.Service.DeleteArticle(ID);
                    RefreshDataGrid(); 
                }
            }
        }



         protected void btnSearch_Click(object sender, EventArgs e)
           {

               RefreshDataGrid();
           }

         protected void btn_add_Click(object sender, EventArgs e)
         {

                 buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
                 btn_add.Attributes.Add("onClick", "ShowNewModal();");
         }


         protected void RefreshDataGrid()
         {
             this.GridView1.DataSource = null;
             using (ServiceProxy Srv = new ServiceProxy())
             {

                 GridView1.DataSource = Srv.Service.GetArticleToDisplay();
                 GridView1.DataBind();

                 buttonNoUse.Click += new EventHandler(this.btnSearch_Click);


             }
         
         }

         protected void test(object sender, GridViewPageEventArgs e)
         {
             
             GridView1.PageIndex = e.NewPageIndex ;
             RefreshDataGrid();
         }



    }
}